import { Filds } from '@/common/type';
import { riskStateCount, queryPage, queryLocationList, queryOptionList, queryInfoById, create, update } from './service';
import { queryDictList } from '@/services/dict';
import { routerRedux } from 'dva/router';
import { DICT } from './enum';
import { message } from 'antd';

export default {
  namespace: 'risk',

  state: {
    todoList: { data: [] },
    doneList: { data: [] },
    details: [],
    todoCond: {},
    doneCond: {},
    dict: {},
    stateCount: {}, //隐患状态统计
  },

  effects: {

    //隐患统计
    *queryStateCount(_, { call, put }) {
      const response = yield call(riskStateCount)
      yield put({ type: 'setRiskStateCount', payload: { countData: response } })
    },

    *updateTodoCond({ payload }, { put }) {
      yield put({ type: 'setTodoCond', payload });
      yield put({ type: 'queryTodoPage' })
    },
    *updateDoneCond({ payload }, { put }) {
      yield put({ type: 'setDoneCond', payload });
      yield put({ type: 'queryDonePage' })
    },
    *updateCoundCond({ payload }, { put }) {
      yield put({ type: 'setCoundCond', payload });
      yield put({ type: 'queryCountPage' })
    },
    *queryTodoPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.risk.todoCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'setTodoPage', payload: { list, total } })
    },
    *queryDonePage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.risk.doneCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      params.state = 32;
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'setDonePage', payload: { list, total } })
    },

    //建筑位置数据开始
    *optionLocation({ payload }, { call, put }) {
      const response = yield call(queryOptionList, payload);
      yield put({ type: 'getOptionData', payload: response })
    },
    *areaLocation({ payload }, { call, put }) {
      const response = yield call(queryOptionList, payload);
      yield put({ type: 'areaOption', payload: response });
    },
    *deviceLocation(_, { call, put }) {
      const response = yield call(queryLocationList);
      yield put({ type: 'getLocationData', payload: response })
    },
    //建筑位置数据结束
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: {} });
      yield put({ type: 'queryPage' })
    },
    *updateCond({ payload }, { put }) {
      yield put({ type: 'setCond', payload });
      yield put({ type: 'queryPage' })
    },
    *queryPage({ payload = { pageNum: 1, pageSize: 10 } }, { call, put, select }) {
      const cond = yield select(state => state.risk.cond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;

      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      yield put({ type: 'setPage', payload: { list, total: +total } })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload.recordId);
      yield put({ type: 'setInfo', payload: response })
    },
    *queryEditInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload.id);
      const items: Filds = {};
      payload.keys.forEach((item: string) => {
        items[item] = response[item];
      });
      yield put({ type: 'setEditInfo', payload: items })
    },
    *create({ payload }, { call, put }) {
      const ans = yield call(create, payload);
      if (ans) {
        message.success(ans);
        yield put(routerRedux.push('/data/risk'));
      }
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put(routerRedux.push('/data/risk'));
    },
  },

  reducers: {
    //生成新的riskStateCount
    setRiskStateCount(state, action) {
      return {
        ...state,
        stateCount: action.payload
      }
    },
    setTodoCond(state, action) {
      return {
        ...state,
        todoCond: action.payload
      }
    },
    setTodoPage(state, action) {
      return {
        ...state,
        todoList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setDoneCond(state, action) {
      return {
        ...state,
        doneCond: action.payload
      }
    },
    setDonePage(state, action) {
      return {
        ...state,
        doneList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    getLocationData(state, action) {
      return {
        ...state,
        locationData: action.payload
      }
    },
    getOptionData(state, action) {
      return {
        ...state,
        optionData: action.payload
      }
    },
    areaOption(state, action) {
      return {
        ...state,
        areaData: action.payload
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setEditInfo(state, action) {
      return {
        ...state,
        editDetails: action.payload
      }
    },
  }
}
